﻿using System.Reflection;

namespace Xioa_UI.Service;

public class FetchContainerServiceProperty
{
    /// <summary>
    /// Property Attributes And Ioc Used Initialeze
    /// </summary>
    /// <param name="loactortor"></param>
    /// <param name="serviceProvider"></param>
    /// <typeparam name="T"></typeparam>
    /// <exception cref="Exception"></exception>
    public static void FetchContainerServiceAllProperty<T>(T? loactortor, IServiceProvider? serviceProvider)
    {
        if (loactortor is null || serviceProvider is null)
            throw new Exception("IOC Null Error or T Is Null");
        var session = loactortor.GetType();
        var properties = session
            .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
            .Where(e =>
                e.GetCustomAttributes(true)
                    .Any(v => v.GetType() == typeof(PropertyAttributes)));
        foreach (var property in properties)
        {
            session.GetProperty(property.Name)?.SetValue(loactortor,
                serviceProvider.GetService(property.PropertyType),
                null);
        }
    }
}